﻿using PLCS.Application.Contracts.EquipmentApp.Dtos;
using PLCS.Application.Contracts.StationApp.Dtos;
using PLCS.Client.IService.EquipmentManager;
using PLCS.Domain.Stations;

using Volo.Abp.Application.Dtos;

namespace PLCS.Client.Service.EquipmentManager;

public class StationService : BaseService<StationDto, StationEditDto>, IStationService
{
    public async Task CreateStationListAsync(IEnumerable<StationEditDto> stations)
    {
        RequestParam requestParam = new RequestParam(method: Method.Post, route: $"station/list", parameter: stations);
        var apiRes = await restClient.ExecuteAsync(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
    }

    public async Task<StationDto> GetByStationNoAsync(string stationNo)
    {
        RequestParam requestParam = new RequestParam(method: Method.Get, route: $"station/bySName?name={stationNo}");
        var apiRes = await restClient.ExecuteAsync<StationDto>(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
        else
            return apiRes.Data;
    }

    public async Task DeleteAllAsync()
    {
        RequestParam requestParam = new(method: Method.Delete, route: $"station/all");
        var apiRes = await restClient.ExecuteAsync(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
    }

    public async Task<PagedResultDto<StationDto>> GetListBySearchAsync(int skipCount, int maxResultCount, string? stationNo, string? stationType)
    {
        var route = $"station/bySearch?SkipCount={skipCount}&MaxResultCount={maxResultCount}";
        if (!string.IsNullOrEmpty(stationNo))
            route = route + $"&stationNo={stationNo}";
        if (!string.IsNullOrEmpty(stationType))
            route = route + $"&stationType={stationType}";

        RequestParam requestParam = new(method: Method.Get, route: route);
        var apiRes = await restClient.ExecuteAsync<PagedResultDto<StationDto>>(requestParam);
        if (apiRes.Status)
            return apiRes.Data;
        else
            return new PagedResultDto<StationDto>();
    }

    public async Task<IEnumerable<StationDto>> GetBySNameFuzzyAsync(string stationNo)
    {
        RequestParam requestParam = new RequestParam(method: Method.Get, route: $"station/bySNameFuzzy?name={stationNo}");
        var apiRes = await restClient.ExecuteAsync<IEnumerable<StationDto>>(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
        else
            return apiRes.Data;
    }
}